package com.yyfax.common.recyclerview.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import com.airbnb.lottie.LottieAnimationView;
import com.yyfax.common.R;

public class HeaderRefreshView extends LinearLayout implements RefreshTrigger {
    public int mMeasuredHeight;
    private LottieAnimationView mRotationView;
    private boolean mFinish = false;


    public HeaderRefreshView(Context context) {
        this(context, null);
    }

    public HeaderRefreshView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeaderRefreshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setGravity(Gravity.CENTER);
        View view = inflate(context, R.layout.recycler_head_refresh_view, this);
        view.setVisibility(VISIBLE);
        mRotationView = (LottieAnimationView) view.findViewById(R.id.rotation_iv);
        mMeasuredHeight = getMeasuredHeight();

//        mRotationView.setAnimation("loading.json");
//        mRotationView.loop(true);
//        mRotationView.setScale(0.3f);

    }

    @Override
    public void onStart(boolean automatic, int headerHeight, int finalHeight) {
//        mRotationView.startRotation();
        mRotationView.playAnimation();
        mFinish = true;
    }

    @Override
    public void onMove(boolean finished, boolean automatic, int moved) {
        if (finished) {
            if (!mFinish) {
                mFinish = true;
            }
        } else {
            if (mFinish) {
                mFinish = false;
            }
        }
    }

    @Override
    public void onRefresh() {
        if (mFinish) {
            mFinish = false;
        }
    }

    @Override
    public void onRelease() {
        if (mFinish) {
            mFinish = false;
        }
    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onReset() {
//        mRotationView.endRotation();
        mRotationView.cancelAnimation();
        mRotationView.clearAnimation();
    }
}